package v1

import (
	"ginblog/model"
	"ginblog/utils/errmsg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)


//添加分类
func AddCate(c *gin.Context)  {
	//1，查询用户是否存在
	var data model.Category
	_ = c.ShouldBindJSON(&data)
	code = model.CheckCate(data.Name)
	if code== errmsg.SUCCSE{
		//成功就写入
		model.CreateCate(&data)
	}
	if code == errmsg.ERROR_USERNAME_USED {
		code = errmsg.ERROR_USERNAME_USED
	}
	//前面的是网络传输的状态，不止200
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"message":errmsg.GetErrMsg(code),
	})
	//2，新增用户
}
//查询单个分类下的文章

//查询单个目录
func GetOneCate(c *gin.Context)  {
	id,_ := strconv.Atoi(c.Param("id"))
	cate,code := model.GetOneCate(id)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":cate,
	})
}

//查询分类列表
func GetCateList(c *gin.Context)  {
	pageSize,_ := strconv.Atoi(c.Query("pageSize"))
	pageNum,_ := strconv.Atoi(c.Query("pageNum"))
	if pageSize==0   {
		pageSize = -1
	}
	if pageNum==0 {
		pageNum = -1
	}
	data,count := model.GetCateList(pageSize,pageNum)
	code = errmsg.SUCCSE
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"count":count,
		"message":errmsg.GetErrMsg(code),
	})
}
//编辑分类
func UpdateCate(c *gin.Context)  {
	var data model.Category
	id,_ := strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)
	code = model.CheckCate(data.Name)
	if code == errmsg.SUCCSE{
		model.UpdateCate(id,&data)
	}
	if code== errmsg.ERROR_USERNAME_USED {
		c.Abort()
	}

	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"message":errmsg.GetErrMsg(code),
	})
}
//删除分类
func DeleteCate(c *gin.Context)  {
	id,_ := strconv.Atoi(c.Param("id"))

	code = model.DeleteCate(id)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"message":errmsg.GetErrMsg(code),
	})
}